﻿using System.Data;
using Dapper;
using DataServer.ApplicationLib.Entities;
using DataServer.CommonLib.Models.DataConfig;
using Swimj.DataCore;

namespace DataServer.ApplicationLib.Repositories
{
    public class ConfigFieldRepository :
    IConfigFieldRepository
    {
        private readonly IDbConnection _dbConnection;

        public ConfigFieldRepository(IDbConnection dbConnection)
        {
            _dbConnection = dbConnection;
        }

        public async Task<IEnumerable<ConfigFieldEntity>> QueryFieldsByConfigAsync(string configCode, ConfigFieldSearchRequestDto model)
        {
            var sql = @"select field.id,
       field.configid,
       field.fieldcode,
       field.fieldname,
       field.displayname,
       field.valuecolumnmap,
       field.datatype,
       field.sort,
       field.description,
       field.isrequired,
       field.iskey,
       field.isvisible,
       field.issearch,
       field.searchrequired,
       field.ignorectlverify,
       field.defaultvalue,
       field.controltype,
       field.controlcontent
from DS_CONFIGFIELD field
join DS_DATACONFIG config on field.ConfigId = config.Id
where config.ConfigCode = @configCode
order by field.Sort asc";

            return await _dbConnection.QueryAsync<ConfigFieldEntity>
                (sql, new
                {
                    configCode = configCode
                });
        }

        public Task<ConfigFieldEntity> GetAsync(Guid id)
        {
            throw new NotImplementedException();
        }

        public Task InsertAsync(ConfigFieldEntity configFieldEntity)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(ConfigFieldEntity configFieldEntity)
        {
            throw new NotImplementedException();
        }

        public async Task<Tuple<IEnumerable<ConfigFieldEntity>, int>> QueryFieldsByPageConfigAsync(string configCode, ConfigFieldSearchRequestDto model)
        {

            var countSql = @"select count(1) from DS_CONFIGFIELD field
join DS_DATACONFIG config on field.ConfigId = config.Id
where config.ConfigCode = @configCode";

            var totalCount = await _dbConnection.ExecuteScalarAsync<int>(countSql, new { configCode = configCode });

            var sql = @"select field.id,
       field.configid,
       field.fieldcode,
       field.fieldname,
       field.displayname,
       field.valuecolumnmap,
       field.datatype,
       field.sort,
       field.description,
       field.isrequired,
       field.iskey,
       field.isvisible,
       field.issearch,
       field.searchrequired,
       field.ignorectlverify,
       field.defaultvalue,
       field.controltype,
       field.controlcontent
from DS_CONFIGFIELD field
join DS_DATACONFIG config on field.ConfigId = config.Id
where config.ConfigCode = @configCode
order by field.Sort asc
LIMIT @offset,@rowsPerPage";

            var data = await _dbConnection.QueryAsync<ConfigFieldEntity>
                (sql, new
                {
                    configCode = configCode,
                    offset = (model.Page - 1) * model.Limit,
                    rowsPerPage = model.Limit
                });
            return new Tuple<IEnumerable<ConfigFieldEntity>, int>(data, totalCount);
        }
    }
}
